package Text5;

import java.util.*;

public class Main {
    public static int N = 310;
    public static int[] s = new int[N];//前缀和
    public static int[][] f = new int[N][N];//合并区间[i,j]的最小代价

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        for (int i = 1; i <= n; i++) {
            s[i] = in.nextInt();
            s[i] += s[i - 1];
        }
        for (int len = 2; len <= n; len++) {
            for (int i = 1; i + len - 1 <= n; i++) {
                int l = i, r = i + len - 1;
                f[l][r] = Integer.MAX_VALUE;
                for (int k = l; k < r; k++) {
                    f[l][r] = Math.min(f[l][r], f[l][k] + f[k + 1][r] + s[r] - s[l - 1]);
                }
            }
        }
        System.out.println(f[1][n]);
    }
}
